<template>
	<view class="contents"></view>
</template>

<script>
	var main, receiver, filter;
	var _codeQueryTag = false;
	import { BroadcastConfig } from './util.js'
	
	/**
	 * sl-pdaEmploy 
	 * @description pda 扫描组件 引入调用即可
	 * @property { BroadcastConfig } config 页面标题
	 */
	export default {
		name: 'sl-pdaEmploy',
		data() {
			return {
				scanCode: ''
			}
 		},
		props: {
			config: {
				type: Object,
				/**
				 * @returns { BroadcastConfig } 扫描配置
				**/
				default: () => {}
			}
		},
		created: function(option) {
			// #ifdef APP-PLUS
			this.initScan()
			this.startScan();
			// #endif
		}, 
		onHide: function() {
			this.stopScan();
		},
		destroyed: function() {
			/*页面退出时一定要卸载监听,否则下次进来时会重复，造成扫一次出2个以上的结果*/
			this.stopScan();
		},
		methods: {
			initScan() {
				let _this = this;
				let { BroadcastAction, BroadcastLabelKey } = this.config;
				main = plus.android.runtimeMainActivity(); //获取activity  
				var IntentFilter = plus.android.importClass('android.content.IntentFilter');
				
				filter = new IntentFilter();
				
				filter.addAction(BroadcastAction); // 换你的广播动作  
				
				receiver = plus.android.implements('io.dcloud.feature.internal.reflect.BroadcastReceiver', {
					onReceive: function(context, intent) {
						plus.android.importClass(intent);
						let code = intent.getStringExtra(BroadcastLabelKey); // 换你的广播标签  
						//console.log(code)
						_this.queryCode(code);  
					}
				});
			},
			startScan() {
				main.registerReceiver(receiver, filter);
			},
			stopScan() {
				main.unregisterReceiver(receiver);
			},
			queryCode: function(code) {
				//防重复  
				// if (_codeQueryTag) return false;
				// _codeQueryTag = true;
				// setTimeout(function() {
				// 	_codeQueryTag = false;
				// }, 150);
				var id = code
				uni.$emit('scancodedate', {
					code: id
				})
			},
		}
	}
</script>

<style>
	page {
		background-color: #efeff4;
	}
	.contents {
		text-align: center;
	}
</style>
